a . volume( ) ; b . volume( ) ; Suppose object a is of type box and object is of type Sphere then different volume methods will call for a and b. bank . withdraw( 5000 ); atm . withdraw( 5000 ) ; Similarly withdraw method for bank and atm may have different rules. c = a + b ; z = x + y ; Similarly if a b and c are numeric varaibles and x y and z are string variables. The same + operator will perform addition or concatenation
abstract class Shape { abstract public void area(); } class Circle extends Shape { private int R; public Circle(int n) { R=n; } public void area() { double a=3.14*R*R; System.out.println("Area is "+a); } } class Rect extends Shape { private int L,B; public Rect(int m,int n) { L=m; B=n; } public void area() { int a=L*B; System.out.println("Area is "+a); } } class demo { public static void main(String args[]) { Shape p,q; p=new Circle(5); p.area(); q=new Rect(10,20); q.area(); } }
Area is 78.5 Area is 200
abstract class Shape { abstract public void area(); } class Circle extends Shape { private int R; public Circle(int n) { R=n; } public void area() { double a=3.14*R*R; System.out.println("Area is "+a); } } class Rect extends Shape { private int L,B; public Rect(int m,int n) { L=m; B=n; } public void area() { int a=L*B; System.out.pritln("Area is "+a); } } class demo { public static void main(String args[]) { Shape arr[]={ new Circle(5), new Rect(2,4), new Rect(10,5), new Circle(7), new Rect(5,5) }; for(int i=0;i<arr.length;i++) arr[i].area(); } }
Area is 78.5 Area is 8 Area is 50 Area is 153.86 Area is 25
class Circle { private int R; public Circle(int n) { R=n; } public void area() { double a=3.14*R*R; System.out.println("Area is "+a); } } class Rect { private int L,B; public Rect(int m,int n) { L=m; B=n; } public void area() { int a=L*B; System.out.println("Area is "+a); } } class demo { public static void main(String args[]) { Circle a=new Circle(5); if(a instanceof Circle) System.out.println("Circle object"); if(a instanceof Rect) System.out.println("Rect object"); if(a instanceof Object) System.out.println("Object object"); } }
Circle object Object object